package com.slicktechnologies.client.views.home;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.core.shared.GWT;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.CaptionPanel;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.InlineLabel;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.simplesoftwares.client.library.appskeleton.AppMemory;
import com.simplesoftwares.client.library.appstructure.ViewContainer;
import com.simplesoftwares.client.library.mywidgets.MyInlineLabel;
import com.slicktechnologies.client.services.CsvService;
import com.slicktechnologies.client.services.CsvServiceAsync;
import com.slicktechnologies.client.utility.Screen;
import com.slicktechnologies.client.views.contract.ContractPresenter;
import com.slicktechnologies.shared.Contract;

// TODO: Auto-generated Javadoc
/**
 * The Form represents a dashboard maninly for reporting purposes.It shows a summary for the business processes.
 * The summary can be configured depending upon the querry conditions.
 * The class and its presenter can be a common component for which we may write parser.
 * 
 */

public class HomeForm extends ViewContainer implements ClickHandler
{
	
	/** String array repersenting the table header menus*/
	String[] tableheaderbarnames={"New","Download"};
	
	/** Flow panel for holding the Table header menus */
	FlowPanel tableheader;
	public ContractPresenterTableProxy contractTable;
	
	
		
	//public HomeSearchCriteria criteria;
	//private final PersistentServiceAsync persistentService = GWT.create(PersistentService.class);
	
	/**
	 * Instantiates a new home form.
	 */
	public HomeForm()
	{
		//Toggle the App header bar menu , In this case only search should come
		toggleAppHeaderBarMenu();
		//Create the Gui, actual Gui gets created here.
		createGui();
		//Sets the event handling
		
	
  }	
			
	/**
	 * Toggles the app header bar menu.
	 */
	private void toggleAppHeaderBarMenu() {
		InlineLabel[] menus=AppMemory.getAppMemory().skeleton.getMenuLabels();
		for(int k=0;k<menus.length;k++)
		{
			String text=menus[k].getText();
			if(text.contains("Search"))
			{
				menus[k].setVisible(true); 
			}
			else
				menus[k].setVisible(false);  		   
			
		}
		
	}

	/* (non-Javadoc)
	 * @see com.simplesoftwares.client.library.appstructure.ViewContainer#createGui()
	 */
	@Override
	public void createGui() 
	{
		//Create the Four Scroll Panel To hold four types of Screen
		ScrollPanel[] scrollPanes=new ScrollPanel[4];
		//String array to hold captions
		String[]captions={"Service Card"};
		//Screen array to hold new Screen object for the purpose of Redirection
		Screen[]screens={Screen.CONTRACT};	
		
		//Create  Flow Panel,Inside Flow Panel 
		for(int i=0;i<captions.length;i++)
		{
			FlowPanel holder= new FlowPanel();
			tableheader= new FlowPanel();
			tableheader.getElement().setId("tableheader");
			// Provides Table Header which is responsible for new or Download Buttons
			setTableHeaderBar(screens[i]);
			
			tableheader.getElement().getStyle().setMarginTop(10, Unit.PX);
			tableheader.getElement().getStyle().setMarginBottom(10, Unit.PX);
			
			scrollPanes[i]= new ScrollPanel();
			scrollPanes[i].setHeight("600px");
		
			holder.add(tableheader);
			holder.add(scrollPanes[i]);
			//Caption Panel inside which a Flow Panel is added
			CaptionPanel cap= new CaptionPanel(captions[i]);
			cap.add(holder);
			cap.getElement().setClassName("tablecaption");
			//Add caption Panel on Content
		    content.add(cap);
		}
		
		
		
		contractTable = new ContractPresenterTableProxy();
		scrollPanes[0].add(contractTable.content);
		content.getElement().setId("homeview");
	  }
	
	/**
	 * ******************************.
	 *
	 * @param screen the new table header bar
	 */
	/**
	 * Method sticks the header bar on tables depending upon their type.To differntiate between tables
	 * app header bar is introduced.
	 * @param screen
	 */
	
	public void setTableHeaderBar(Screen screen)
	{
		
		
		for(int j=0;j<getTableheaderbarnames().length;j++)
		{
			MyInlineLabel lbl;
			lbl=new MyInlineLabel(getTableheaderbarnames()[j],screen);
            getTableheader().add(lbl);
			lbl.getElement().setId("tableheadermenu");
			lbl.addClickHandler(this);
		}
	}

	
 
 
 
/* (non-Javadoc)
 * @see com.simplesoftwares.client.library.appstructure.ViewContainer#applyStyle()
 */
@Override
public void applyStyle() {
	
}

/**
 * Gets the tableheaderbarnames.
 *
 * @return the tableheaderbarnames
 */
public String[] getTableheaderbarnames() {
	return tableheaderbarnames;
}

/**
 * Sets the tableheaderbarnames.
 *
 * @param tableheaderbarnames the new tableheaderbarnames
 */
public void setTableheaderbarnames(String[] tableheaderbarnames) {
	this.tableheaderbarnames = tableheaderbarnames;
}

/**
 * Gets the tableheader.
 *
 * @return the tableheader
 */
public FlowPanel getTableheader() {
	return tableheader;
}

/**
 * Sets the tableheader.
 *
 * @param tableheader the new tableheader
 */
public void setTableheader(FlowPanel tableheader) {
	this.tableheader = tableheader;
}

/**
 * Gets the lead table.
 *
 * @return the lead table
 */


/**
 * Sets the lead table.
 *
 * @param leadTable the new lead table
 */



/**
 * Gets the contract table.
 *
 * @return the contract table
 */
public ContractPresenterTableProxy getContractTable() {
	return contractTable;
}

/**
 * Sets the contract table.
 *
 * @param contractTable the new contract table
 */
public void setContractTable(ContractPresenterTableProxy contractTable) {
	this.contractTable = contractTable;
}

/* (non-Javadoc)
 * @see com.google.gwt.event.dom.client.ClickHandler#onClick(com.google.gwt.event.dom.client.ClickEvent)
 */
@Override
public void onClick(ClickEvent event) {
	if(event.getSource() instanceof MyInlineLabel)
	{
		MyInlineLabel lbl=(MyInlineLabel) event.getSource();
		redirectOnScreens(lbl);
		
	}
	
}

private void redirectOnScreens(MyInlineLabel lbl)
{
	Screen screen=(Screen) lbl.redirectScreen;
	String title=lbl.getText().trim();
	switch(screen)
	{
	  
			
	  
			
	  case CONTRACT:
		  
			if(title.equals("Download"))
			{
				reactOnDownload();
			}
			
			if(title.equals("New"))
			{
				AppMemory.getAppMemory().setCurrentScreenandChangeProcessName("Sales/Contract",Screen.CONTRACT);
				ContractPresenter.initalize();
			}
			
			break;
			
	  
	default:
		break;
		  
			
			
	}
	
	
 




}




public void reactOnDownload() {
	
	
	ArrayList<Contract> custarray=new ArrayList<Contract>();
	List<Contract> list=(List<Contract>) contractTable.getDataprovider().getList();
	
	custarray.addAll(list);
	CsvServiceAsync csvservice=GWT.create(CsvService.class);
	
	csvservice.setcontractlist(custarray, new AsyncCallback<Void>() {

		@Override
		public void onFailure(Throwable caught) {
			System.out.println("RPC call Failed"+caught);
			
		}

		@Override
		public void onSuccess(Void result) {
			String gwt=com.google.gwt.core.client.GWT.getModuleBaseURL();
			final String url=gwt + "csvservlet"+"?type="+6;
			Window.open(url, "test", "enabled");
		}
	});

}








}

		
	


